﻿using System;
using System.Collections.Generic;
using System.Text;

namespace adb
{
    /// <summary>
    /// 时间扩展类型
    /// </summary>
    public static class DateTimeExt
    {
        /// <summary>
        /// 是否同一天
        /// </summary>
        /// <param name="time1">时间一</param>
        /// <param name="time2">时间二</param>
        /// <returns></returns>
        public static bool IsSameDay(this DateTime time1, DateTime? time2) { 

            return time2.HasValue && time1.Date == time2.Value.Date;
        }
        /// <summary>
        /// 是否同年同月
        /// </summary>
        /// <param name="time1">时间一</param>
        /// <param name="time2">时间二</param>
        /// <returns></returns>
        public static bool IsSameMonth(this DateTime time1, DateTime? time2) {
            return time2.HasValue && time1.Year == time2.Value.Year && time2.Value.Month == time1.Month;
        }
        /// <summary>
        /// 是否同月不同年
        /// </summary>
        /// <param name="time1">时间一</param>
        /// <param name="time2">时间二</param>
        /// <returns></returns>
        public static bool IsSameMonth2(this DateTime time1, DateTime? time2)
        {
            return time2.HasValue && time1.Year != time2.Value.Year && time2.Value.Month == time1.Month;
        }

        /// <summary>
        /// 是否同年
        /// </summary>
        /// <param name="time1">时间一</param>
        /// <param name="time2">时间二</param>
        /// <returns></returns>
        public static bool IsSameYear(this DateTime time1, DateTime? time2) {
            return time2.HasValue && time1.Year == time2.Value.Year;
        }
        /// <summary>
        /// 是否在两个时间段之间,这里只算小时之后的时间
        /// </summary>
        /// <param name="time1">时间1</param>
        /// <param name="time2">时间2</param>
        /// <param name="time3">时间3</param>
        /// <returns></returns>
        public static bool IsBetweenTimeOfDay(this DateTime time1, DateTime time2, DateTime time3) {
            var t1 = new TimeSpan(time1.Hour, time1.Minute, time1.Second);
            var t2 = new TimeSpan(time2.Hour, time2.Minute, time2.Second);
            var t3 = new TimeSpan(time3.Hour, time3.Minute, time3.Second);
            return t2 <= t1 && t2 > t1;
        }
    }
}
